package com.breaking.second.activity.pager.adapter;

import static android.os.Build.VERSION_CODES.R;

import android.content.Context;
import android.content.Intent;
import android.text.format.DateUtils;
import android.view.View;

import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.breaking.second.R;
import com.breaking.second.activity.GoodsDetailActivity;
import com.breaking.second.databinding.ItemGoodsBinding;
import com.breaking.second.entity.GoodsEntity;
import com.breaking.second.utils.compat.RecyclerCompat;
import com.bumptech.glide.Glide;

import java.util.List;

public class GoodsAdapter extends BaseAdapter<ItemGoodsBinding> {
    private final List<GoodsEntity> goodsEntities;

    public GoodsAdapter(Context context, List<GoodsEntity> goodsEntities) {
        super(context);
        this.goodsEntities = goodsEntities;
    }

    @Override
    public void onBindViewHolder(ItemGoodsBinding binding, int position) {
        GoodsEntity goodsEntity = goodsEntities.get(position);
        Glide.with(getContext()).load(goodsEntity.getAvatar()).placeholder(com.breaking.second.R.mipmap.ic_launcher).into(binding.avatar);
        binding.content.setText(goodsEntity.getContent());
        binding.price.setText(String.valueOf(goodsEntity.getPrice()));
        binding.username.setText(goodsEntity.getUsername());
        ImageAdapter imageAdapter = new ImageAdapter(getContext(), goodsEntity.getImageUrlList());
        binding.images.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        binding.images.setAdapter(imageAdapter);
        RecyclerCompat.SpacesItemDecoration(binding.images);
        binding.addr.setText(String.format("%s.%s", DateUtils.getRelativeTimeSpanString(Long.parseLong(goodsEntity.getCreateTime())).toString(), goodsEntity.getAddr()));
        binding.getRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), GoodsDetailActivity.class);
                intent.putExtra("id", goodsEntity.getId());
                getContext().startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return goodsEntities.size();
    }
}
